using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Q.Framework.WebCode;
using System.Web.UI.WebControls;
using System.Data.Objects.DataClasses;
using Q.Framework.Data;
using Q.Framework.WebControls;
using System.Data.Objects;

namespace Q.Framework.WebCode
{
    public class BasePageCrudCodeDescriptionStatus<T> : PageBase
        where T : class, new()
    {
        private DAO<T> controller;

        public DAO<T> Controller
        {
            get
            {
                if (controller == null)
                    controller = new DAO<T>();

                return controller;
            }
        }

        private T business;

        public T Business
        {
            get
            {
                if (business == null)
                {
                    if (Request.QueryString["ID"] != null)
                        business = DAOGeneric.GetById<T>(Convert.ToInt32(Request.QueryString["ID"]));
                    else
                        business = Activator.CreateInstance<T>();
                }

                return business;
            }
            set { business = value; }
        }

        private WebControls.QTextBox txtDescricao;

        public QTextBox TxtDescricao
        {
            get
            {
                if (txtDescricao == null)
                    txtDescricao = ((CrudCodeDescritionStatusBasePage)Master).TxtDescricao;
                return txtDescricao;
            }
            set { txtDescricao = value; }
        }

        private GridView grdResultado;

        public GridView GrdResultado
        {
            get
            {
                if (grdResultado == null)
                    grdResultado = ((CrudCodeDescritionStatusBasePage)Master).GrdResultado;
                return grdResultado;
            }
            set { grdResultado = value; }
        }

        private TextBox txtID;

        public TextBox TxtID
        {
            get
            {
                if (txtID == null)
                    txtID = ((CrudCodeDescritionStatusBasePage)Master).TxtCodigo;
                return txtID;
            }
            set { txtID = value; }
        }

        private Label lblDescricao;

        public Label LblDescricao
        {
            get
            {
                if (lblDescricao == null)
                    lblDescricao = ((CrudCodeDescritionStatusBasePage)Master).LblDescricao;
                return lblDescricao;
            }
            set { lblDescricao = value; }
        }

        private RadioButtonList rblStatus;

        public RadioButtonList RblStatus
        {
            get
            {
                if (rblStatus == null)
                    rblStatus = ((CrudCodeDescritionStatusBasePage)Master).RblStatus;
                return rblStatus;
            }
            set { rblStatus = value; }
        }

        public String NomeCampoDescritivo
        {
            get { return ((CrudCodeDescritionBasePage)Master).NomeCampoDescritivo; }
            set { ((CrudCodeDescritionBasePage)Master).NomeCampoDescritivo = value; }
        }

        protected override void OnInit(EventArgs e)
        {
            if (!this.DesignMode)
            {
                base.PageBasePage_Load(e);

                ((CrudCodeDescritionStatusBasePage)Master).Save += Save;
                ((CrudCodeDescritionStatusBasePage)Master).Delete += Delete;
                ((CrudCodeDescritionStatusBasePage)Master).New += New;
                ((CrudCodeDescritionStatusBasePage)Master).PreencherOutrasInformacoes += new EventHandler(PreencherOutrasInformacoes);
                ((CrudCodeDescritionStatusBasePage)Master).SetOtherProperties += new EventHandler(SetOtherProperties);
            }

            base.OnInit(e);
        }

        protected void SearchBusiness(object sender, EventArgs e)
        {
            Business = DAOGeneric.GetById<T>(Int32.Parse(TxtID.Text));
        }

        public virtual void PreencherOutrasInformacoes(object sender, EventArgs e)
        {

        }

        public virtual void SetOtherProperties(object sender, EventArgs e)
        {

        }

        protected override void OnLoadComplete(EventArgs e)
        {
            if (!IsPostBack)
            {
                GrdResultado.DataSource = Controller.SelectAll().Select(s => new
                {
                    ID = (Int32)s.GetType().GetProperty("ID").GetValue(s, null),
                    Descricao = (String)s.GetType().GetProperty("Descricao").GetValue(s, null),
                    DescricaoSituacao = s.GetType().GetProperty("Situacao") == null ? String.Empty : s.GetType().GetProperty("Situacao").GetValue(s, null).ToString() == "I" ? "Inativo" : "Ativo",
                    Situacao = s.GetType().GetProperty("Situacao") == null ? String.Empty : s.GetType().GetProperty("Situacao").GetValue(s, null)
                });

                GrdResultado.DataBind();
                if (Request.QueryString["ID"] != null)
                {
                    TxtID.Text = Business.GetType().GetProperty("ID").GetValue(Business, null).ToString();
                    TxtDescricao.Text = Business.GetType().GetProperty("Descricao").GetValue(Business, null).ToString();
                    RblStatus.SelectedValue = typeof(T).GetProperty("Situacao").GetValue(Business, null).ToString();
                }
            }

            base.OnLoadComplete(e);
        }

        protected override void OnLoad(EventArgs e)
        {

        }

        protected void New(object sender, EventArgs e) { Redirect(GetPage()); }

        protected void Save(object sender, EventArgs e)
        {
            Business.GetType().GetProperty("Descricao").SetValue(Business, TxtDescricao.Text, null);
            typeof(T).GetProperty("Situacao").SetValue(Business, RblStatus.SelectedValue, null);

            Controller.Save(Business);
        }

        protected virtual void Delete(object sender, EventArgs e)
        {
            Business = DAOGeneric.GetById<T>(Convert.ToInt32(((CrudCodeDescritionStatusBasePage)Master).Codigo));
            Business.GetType().GetProperty("Situacao").SetValue(Business, "I", null);
            Controller.Save(Business);
        }

    }
}